class Solution {
public:
    string compressString(string S) {
        string ns = "";
        char c = '0';
        int count = 0;
        for (char item : S) {
            if (c == item) {
                count++;
            } else {
                if (count > 0) {
                    ns += to_string(count);
                }
                count = 1;
                ns += item;
                c = item;
            }
        }
        if (count > 0) {
            ns += to_string(count);
        }
        return ns.size() < S.size() ? ns : S;
    }
};